package org.example.wu_and_dian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.entity.Entity;
import org.example.wu_and_dian.mapper.EntityMapper;
import org.example.wu_and_dian.service.EntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author SS2613
* @description 针对表【entity(用于存储实体类的信息)】的数据库操作Service实现
* @createDate 2025-03-18 14:19:09
*/
@Service
public class EntityServiceImpl extends ServiceImpl<EntityMapper, Entity>
    implements EntityService {
    @Autowired
    private EntityMapper entityMapper;

    @Override
    public List<Entity> getByDocumentId(String documentId) {
        return baseMapper.getByDocumentId(documentId);
    }

    @Override
    public boolean deleteById(String documentId,String id) {
        return baseMapper.deleteById(documentId,id) > 0;
    }

    @Override
    public AjaxResult createEntityInstance(Entity entity) {
        if (entity == null || entity.getDocumentId() == null) {
            return AjaxResult.error("document_id 不能为空");
        }
        int result =entityMapper.createEntityInstance(entity);
        if(result>0){
            return AjaxResult.success("创建实体实例成功");
        }else {
            return AjaxResult.error("创建实体实例失败");
        }
    }

    @Override
    public AjaxResult updateEntityInstancesByEntityId(String documentId, String entityId, Boolean isChecked) {
        if (documentId == null || entityId == null) {
            return AjaxResult.error("document_id 和 entity_id 不能为空");
        }

        int result = entityMapper.updateEntityInstancesByEntityId(documentId, entityId, isChecked);
        if (result > 0) {
            return AjaxResult.success("更新实体实例成功");
        } else {
            return AjaxResult.error("更新实体实例失败");
        }
    }

    @Override
    public AjaxResult updateEntityInstance(String instanceId, String documentId, Boolean isChecked) {
        if (documentId == null || instanceId == null) {
            return AjaxResult.error("document_id 和 entity_id 不能为空");
        }
        int result=entityMapper.updateEntityInstance(instanceId, documentId, isChecked);
        if(result>0){
            return AjaxResult.success("更新实体实例成功");
        }else {
            return AjaxResult.error("更新实体实例失败");
        }
    }

    @Override
    public AjaxResult deleteEntityInstancesByEntityId(String documentId, String instanceId) {
        if (documentId == null || instanceId == null) {
            return AjaxResult.error("document_id 和 entity_id 不能为空");
        }
        int result = entityMapper.deleteEntityInstancesByEntityId(documentId, instanceId);
        if (result > 0) {
            return AjaxResult.success("删除实体实例成功");
        } else {
            return AjaxResult.error("删除实体实例失败");
        }
    }
}




